<template>
  <div v-loading="loading" :class="className" :style="{height: height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons')
import resize from '@/views/deal/report/chart/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'dealPhase'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      loading: false,
      optionData: {
        dateList: []
      }
    }
  },
  watch: {
    chartData() {
      const data = {
        amount: [],
        count: [],
        nodeName: []
      }
      this.chartData.forEach(item => {
        if (item.count && item.count > 0) {
          data.amount.push(item.amount / 1000000)
          data.count.push(item.count)
          data.nodeName.push(this.$t(item.nodeName))
        }
      })
      this.chart.setOption({
        xAxis: [
          {
            data: data.nodeName
          }
        ],
        series: [
        {
          name: this.$t('数量(个)'),
          data: data.count
        }]
      })
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  mounted() {
    this.initChart()
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, null)

      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              color: '#dddc6b'
            }
          }
        },
        grid: {
          left: '10',
          top: '70',
          right: '30',
          bottom: '10',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: ['5%', '5%'],
            axisLabel: {
              textStyle: {
                color: '#fff',
                fontSize: 12
              }
            },
            axisLine: {
              lineStyle: {
                color: 'rgba(255,255,255,.1)'
              }
            },
            data: ['项目1', '项目2', '项目3', '项目4', '项目5']
          }
        ],
        yAxis: [
          {
            name: this.$t('数量(个)'),
            type: 'value',
            axisTick: { show: false },
            min: 0,
            splitLine: {
              lineStyle: {
                color: 'rgba(255,255,255,.2)'
              }
            },
            axisLine: {
              show: false
            },
            axisLabel: {
              color: '#fff'
            },
            splitArea: {
              areaStyle: {
                color: 'rgba(255,255,255,0)'
              }
            }
          }
        ],
        series: [
          {
            name: this.$t('金额(百万)'),
            type: 'bar',
            barWidth: '20px',
            itemStyle: {
              normal: {
                color: function(params) {
                  var colorList = ['#5470C6', '#91CC75', '#FAC858', '#EE6666', '#73C0DE', '#3BA272']
                  return colorList[params.dataIndex]
                }
              }
            },
            data: []
          },
          {
            name: this.$t('数量(个)'),
            type: 'line',
            data: []
          }
        ],
        animationEasing: 'cubicInOut',
        animationDuration: 3000
      })
    }
  }
}
</script>
